package com.ld.control.business.data.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.control.business.data.mapper.TemporaryMapMapper;
import com.ld.control.business.data.model.TemporaryMapModel;
import com.ld.control.business.data.service.TemporaryMapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wangfeng
 * @since 2024-09-04
 */
@Service
public class TemporaryMapServiceImpl extends ServiceImpl<TemporaryMapMapper, TemporaryMapModel> implements TemporaryMapService {

    @Autowired
    private TemporaryMapMapper temporaryMapMapper;

    @Override
    public void setString(String key, String value) {
        TemporaryMapModel dbModel = temporaryMapMapper.selectByBusinessIdAndDataKey(null, key);
        if (!ObjectUtils.isEmpty(dbModel)) {
            dbModel.setDataValue(value);
            dbModel.setUpdateTime(new Date());
            temporaryMapMapper.updateById(dbModel);
        } else {
            TemporaryMapModel temporaryMapModel = new TemporaryMapModel();
            temporaryMapModel.setDataKey(key);
            temporaryMapModel.setDataValue(value);
            temporaryMapModel.setCreateTime(new Date());
            temporaryMapMapper.insert(temporaryMapModel);
        }
    }

    @Override
    public void setString(String businessId, String key, String value) {

    }

    @Override
    public String getString(String key) {
        TemporaryMapModel temporaryMapModel = temporaryMapMapper.selectByBusinessIdAndDataKey(null, key);
        if (!ObjectUtils.isEmpty(temporaryMapModel)) {
            return temporaryMapModel.getDataValue();
        }
        return "";
    }

    @Override
    public String getString(String businessId, String key) {
        return "";
    }

    @Override
    public void setInt(String key, int value) {
        setString(key, String.valueOf(value));
    }

    @Override
    public Integer getInt(String key) {
        String value = getString(key);
        if (ObjectUtils.isEmpty(value)) {
            return null;
        }
        return Integer.parseInt(value);
    }
}
